package org.practice.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.practice.dao.OrderDao;
import org.practice.model.Address;
import org.practice.model.Item;
import org.practice.model.Order;
import org.practice.service.CartService;
import org.practice.service.UserService;
import org.practice.service.bo.CartBO;
import org.practice.service.bo.CartItemBO;
import org.practice.util.Constant;
import org.practice.util.ShopDaoFactory;
import org.practice.util.ShopServiceFactory;
import org.practice.util.UUIDUtil;

@SuppressWarnings("serial")
public class OrderAction extends BaseAction{
	private Address address = new Address();
	private List<Address> addrList=new ArrayList<Address>();
	public String recipient()throws Exception {
		return SUCCESS;
	}
	public void address()throws Exception {
		Thread.sleep(2000);
		OrderDao orderDao = ShopDaoFactory.createOrderDao();
		addrList=orderDao.findOrderAddressByUser(getUser());
		
		StringBuffer json=new StringBuffer("[");
		for (Iterator<Address> iterator = addrList.iterator(); iterator.hasNext();) {
			Address addr =iterator.next();
			json.append("{");
			json.append("fullAddress:'"+addr.getFullAddress()+"',");
			json.append("receiveName:'"+addr.getReceiveName()+"',");
			json.append("postalCode:'"+addr.getPostalCode()+"',");
			json.append("mobile:'"+addr.getMobile()+"'");
			json.append("}");
			if(iterator.hasNext()){
				json.append(",");
			}
		}
		json.append("]");
		HttpServletResponse response=getResponse();
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().print(json.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	public String confirm() throws Exception {
		getSession().setAttribute("address", address);
		return SUCCESS;
	}
	/**
	 * 下订单place order
	 */
	@Override
	public String execute() throws Exception {
		HttpSession session = getSession();
		CartBO cart = (CartBO) session.getAttribute(Constant.SESSION_KEY_CART);
		Address address = (Address) session.getAttribute("address");
		if (cart == null||address==null) {
			return FAIL;
		}

		OrderDao orderDao = ShopDaoFactory.createOrderDao();
		Order order = new Order();
		
		order.setAddress(address);
		
		UserService userService = ShopServiceFactory.createUserService();
		order.setUser(userService.findUserById(cart.getUserId()));
		
		order.setOrderTime(System.currentTimeMillis());
	
		order.setOrderPrice(cart.getTotalPrice());
		order.setTotalPrice(order.getOrderPrice()+0);

		order.setStatus(Boolean.TRUE);
		order.setOrderNumber(UUIDUtil.nextCode());
		
		List<CartItemBO> cartItems = cart.getEffectiveCartItems();
		Set<Item> items = new HashSet<Item>();
		for (int i = 0; i < cartItems.size(); i++) {
			Item item = new Item();
			CartItemBO ct = (CartItemBO) cartItems.get(i);
			item.setProduct(ct.getProduct());
			item.setAmount(ct.getProduct().getDangPrice() * ct.getNumber());
			item.setDangPrice(ct.getProduct().getDangPrice());
			item.setNum(ct.getNumber());
			item.setOrder(order);
			items.add(item);
		}

		order.setItems(items);
		
		orderDao.addOrder(order);
		
		
		
		
		session.setAttribute("order", order);
		CartService service=ShopServiceFactory.createCartService();
		service.clear(cart);
		return SUCCESS;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
	public List<Address> getAddrList() {
		return addrList;
	}
	public void setAddrList(List<Address> addrList) {
		this.addrList = addrList;
	}
	
}
